package com.ww.springboot.boot.thread.study.tools;

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2023-08-04 10:03
 */
public class 线程名获取线程 {

    private final static List<Thread> list = new ArrayList<>();
    private final static ExecutorService executorService = new ThreadPoolExecutor(8, 8, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>()
            ,new CustomizableThreadFactory("我的线程池"));

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            Thread thread = new Thread(() -> {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

            executorService.submit(thread);
            list.add(thread);
        }

        Thread.sleep(1000);

        for (int i = 0; i < 10; i++) {

            Thread.sleep(1000);
            new Thread(()->{
                method1();
            }).start();

            // method2();
        }


    }

    private static void method2() {
        Map<Thread, StackTraceElement[]> stackTraceMap = Thread.getAllStackTraces();
        for (Thread thread : stackTraceMap.keySet()) {
            System.out.println(thread.getName());
        }
    }

    private static void method1() {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        Thread[] threads = new Thread[group.activeCount()];
        group.enumerate(threads);

        for (Thread thread : threads) {
            System.out.println(thread.getName());
        }
    }
}
